/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : nicepay_billkey.js                                 
*  Description  : 결제
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-21  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	
	
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		// 해쉬코드
		, getHashString: function(arg){
			var thisClass = this;
			
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "setPayData";
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, {
				"reserv_seq": arg["예약코드"]
				, "reserv_type": arg["예약타입"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					
					res["결과값"] = {
						"mid": _util.nvl(obj["mid"])
						, "mkey": _util.nvl(obj["mkey"])
						, "edidate": _util.nvl(obj["edidate"])
						, "hashcode": _util.nvl(obj["hashcode"])
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 보증예약
		, nicepaySubmit: function(arg){
			var thisClass = this;
			
			arg = arg || {};
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "reservAccess";
			var options = {
				"reserv_status": "G"
			}
			
			for(var k in arg){
				if(k == "콜백") continue;
				
				options[k] = arg[k];
			}
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		
		this.q_reserve = null;
		this.openerControl = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			this.q_reserve = _util.getParam("q_reserve");
			
			if(opener){
				if(opener.document.objControl){
					this.openerControl = opener.document.objControl;
				}
			}
			
//			if(!this.openerControl){
//				self.close();
//				return;
//			}
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				
				return _gf.getMyInfoData();
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			
		}
		, _start: function(){
			var thisClass = this;
			
			this.objComm.getHashString({
				"예약코드": this.q_reserve
				, "예약타입": "R"
				, "콜백": function(ret){
					thisClass.finishHashString(ret);
				}
			});
		}
		// 해쉬코드 완료
		, finishHashString: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			var thisClass = this;
			
			var $f = $("form");
			var d = ret["결과값"];
			var mi = _gf.getMyInfoData();
			
			var MID = d["mid"];
			var Moid = this.q_reserve;
			
			var MallReserved = "q_reserve!@!@" + this.q_reserve + "@!@!mid!@!@" + MID + "@!@!mkey!@!@" + d["mkey"];
			
			document.charset = "euc-kr";
			
			
			$f.attr("acceptCharset", "euc-kr");
			$f.append('<input type="hidden" name="GoodsName" value="sky72 골프인예약">');
			$f.append('<input type="hidden" name="BuyerName" value="' + mi["이름"] + '">');
			$f.append('<input type="hidden" name="ReturnUrl" value="http://' + document.domain + '/nicepay_billkey_result">');
			$f.append('<input type="hidden" name="MID" value="' + MID + '">');
			$f.append('<input type="hidden" name="Moid" value="' + Moid + '">');
			$f.append('<input type="hidden" name="BuyerTel" value="' + mi["휴대폰"] + '">');
			$f.append('<input type="hidden" name="MallReserved" value="' + MallReserved + '">');
			$f.append('<input type="hidden" name="LogoUrl" value="">');
			
			$f.attr("action", "https://web.nicepay.co.kr/billing/step1.jsp");
			$f.attr("id", "idPermissionForm");
			$f.trigger("submit");
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});